package pers.lonestar.framework.utils.support.utils;

import java.util.LinkedList;
import java.util.List;

import pers.lonestar.framework.utils.support.metadata.read.ReadSheet;

public class SheetUtil {
    public static ReadSheet match(List<ReadSheet> readSheetList, ReadSheet parameterSheet) {
        for (ReadSheet readSheet : readSheetList) {
            if (parameterSheet.getSheetIndex() == null && StringUtil.isEmpty(parameterSheet.getSheetName())) {
                parameterSheet.setSheetIndex(0);
            }
            boolean match = (parameterSheet.getSheetIndex() != null) && (readSheet.getSheetIndex().equals(parameterSheet.getSheetIndex()));
            if (!match) {
                String parameterSheetName = parameterSheet.getSheetName();
                if (!StringUtil.isEmpty(parameterSheetName)) {
                    match = readSheet.getSheetName().trim().equals(parameterSheetName.trim());
                }
            }
            if (match) {
                parameterSheet.setInputStream(readSheet.getInputStream());
                parameterSheet.setTagDeque(new LinkedList<>());
                return parameterSheet;
            }
        }
        return null;
    }
}
